package handler

import (
	"ecom-mgr/cache"
	"ecom-mgr/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
)

func GoodAdd(c *gin.Context) {
	good_code := c.PostForm("good_code")
	good_name := c.PostForm("good_name")
	good_price := c.PostForm("good_price")
	data := models.Good{
		GoodCode:  good_code,
		GoodName:  good_name,
		GoodPrice: good_price,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "商品添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "商品添加成功",
	})
	return
}
func GoodOrderAdd(c *gin.Context) {
	gid, _ := strconv.Atoi(c.PostForm("gid"))
	order_code := c.PostForm("order_code")
	order_price, _ := strconv.Atoi(c.PostForm("order_price"))
	data := models.GoodOrder{
		Gid:        gid,
		OrderCode:  order_code,
		OrderPrice: order_price,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "商品订单添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "商品订单添加成功",
	})
	return
}
func GoodOrderList(c *gin.Context) {
	/*
		SELECT
		  good_orders.id AS OrderId,
		  good_orders.order_code AS OrderCode,
		  good_orders.order_price AS OrderPrice,
		  goods.good_code AS GoodCode,
		  goods.good_name AS GoodName
		FROM
		  good_orders
		  LEFT JOIN goods ON goods.id = good_orders.gid
	*/
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "1"))
	offset := (page - 1) * size
	var goodOrderList []models.GoodOrderList
	redis_key := "good_order_page_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("good_orders").
			Select(
				"good_orders.id AS OrderId",
				"good_orders.order_code AS OrderCode",
				"good_orders.order_price AS OrderPrice",
				"goods.good_code AS GoodCode",
				"goods.good_name AS GoodName",
			).Joins("LEFT JOIN goods ON goods.id = good_orders.gid").
			Offset(offset).
			Limit(size).
			Debug().
			Find(&goodOrderList)
	} else {
		json.Unmarshal([]byte(cacheData), &goodOrderList)
	}
	var total int64
	models.DB.Table("good_orders").Count(&total)
	c.JSON(200, gin.H{
		"code":  500,
		"msg":   "ok",
		"data":  goodOrderList,
		"total": total,
	})
	return
}
func GoodOrderDetail(c *gin.Context) {
	
	id := c.Query("id")
	var goodOrderDetail []models.GoodOrderList
	redis_key := "good_order_detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("good_orders").
			Select(
				"good_orders.id AS OrderId",
				"good_orders.order_code AS OrderCode",
				"good_orders.order_price AS OrderPrice",
				"goods.good_code AS GoodCode",
				"goods.good_name AS GoodName",
			).Joins("LEFT JOIN goods ON goods.id = good_orders.gid").
			Where("goods.id=?", id).
			Debug().
			Find(&goodOrderDetail)
	} else {
		json.Unmarshal([]byte(cacheData), &goodOrderDetail)
	}
	c.JSON(200, gin.H{
		"code": 500,
		"msg":  "ok",
		"data": goodOrderDetail,
	})
	return
}
